﻿using System;
using NUnit.Framework;
using System.Collections.Generic;

namespace NMA.Infrastructure.NHibernate.Tests
{
    using NMA.Infrastructure.NHibernate.DomainObject;
    using NMA.Infrastructure.Configuration;
    using NMA.Infrastructure.NHibernate.Bootstrapper;
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.Repository;
    using NMA.Domain.Model.Repository;
    using System.Collections;

    [TestFixture]
    public class TestingBase
    {
        protected IConfigurationManager ConfigurationManager { get; private set; }

        protected IList<Category> CategoryList { get; private set; }
        protected IList<News> NewsList { get; private set; }
        protected IList<Poll> PollList { get; private set; }
        protected IList<Audit> AuditList { get; private set; }
        protected IList<User> UserList { get; private set; }
        protected IList<Resources> ResourceList { get; private set; }

        protected ICategoryRepository CategoryRepository { get; private set; }
        protected INewsRepository NewsRepository { get; private set; }
        protected IPollRepository PollRepository { get; private set; }
        protected IAuditRepository AuditRepository { get; private set; }
        protected IUserRepository UserRepository { get; private set; }
        protected IResourceRepository ResourceRepository { get; private set; }

        [SetUp]
        public virtual void SetupContext()
        {
            Before_each_test();
        }

        [TearDown]
        public virtual void TeardownContext()
        {
            After_each_test();
        }

        protected virtual void Before_each_test()
        {
            ConfigurationManager = new MockObject.MockConfiguration();
            IList<Type> registries = new List<Type>();
            registries.Add(typeof(CoreRegistry));

            new BootStrapper(
                registries,
                ConfigurationManager,
                "NMAConnectionString",
                "NMA.Infrastructure.NHibernate", "NMA.Application", "NMA.Infrastructure.AutoMapper");

            AddCategory();
            AddNews();
            AddPoll();
            AddAudit();
            AddUser();
            AddResources();

            CategoryRepository = new CategoryRepository();
            NewsRepository = new NewsRepository();
            PollRepository = new PollRepository();
            AuditRepository = new AuditRepository();
            UserRepository = new UserRepository();
            ResourceRepository = new ResourceRepository();

            Add_data_to_database();
        }

        protected virtual void After_each_test()
        {
            Delete_data_from_database();

            if (ConfigurationManager != null)
                GC.SuppressFinalize(ConfigurationManager);
        }

        private void Add_data_to_database()
        {
            foreach (var item in CategoryList)
            {
                CategoryRepository.Add(item);	 
            }

            foreach (var item in NewsList)
            {
                NewsRepository.Add(item);
            }

            foreach (var item in PollList)
            {
                PollRepository.Add(item);
            }

            foreach (var item in AuditList)
            {
                AuditRepository.Add(item);
            }

            foreach (var item in UserList)
            {
                UserRepository.Add(item);
            }

            foreach (var item in ResourceList)
            {
                ResourceRepository.Add(item);
            }
        }

        private void Delete_data_from_database()
        {
            foreach (var item in CategoryList)
            {
                CategoryRepository.Remove(item);
            }

            foreach (var item in NewsList)
            {
                NewsRepository.Remove(item);
            }

            foreach (var item in PollList)
            {
                PollRepository.Remove(item);
            }

            foreach (var item in AuditList)
            {
                AuditRepository.Remove(item);
            }

            foreach (var item in UserList)
            {
                UserRepository.Remove(item);
            }

            foreach (var item in ResourceList)
            {
                ResourceRepository.Remove(item);
            }
        }

        private void AddCategory()
        {
            CategoryList = new List<Category>();

            CategoryList.Add(new Category() {
                Id = Guid.NewGuid(),
                Name = "unit test name 1",
                CreatedUser = "unit test created user 1",
                UpdatedUser = "unit test updated user 1",
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now
            });

            CategoryList.Add(new Category()
            {
                Id = Guid.NewGuid(),
                Name = "unit test name 2",
                CreatedUser = "unit test created user 2",
                UpdatedUser = "unit test updated user 2",
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now
            });
        }

        private void AddNews()
        {
            NewsList = new List<News>();

            NewsList.Add(new News()
            {
                Id = Guid.NewGuid(),
                Title = "title 1",
                ShortDescription = "short description 1",
                Content = "content 1",
                IsPublish = true,
                NumberOfView = 0,
                PublishedPerson = "published person 1",
                PublishedDate = DateTime.Now,
                CreatedDate = DateTime.Now,
                CreatedUser = "created user 1",
                UpdatedDate = DateTime.Now,
                UpdatedUser = "created user 1",
                Image = new byte[] { 123, 123, 123 }
            });

            NewsList.Add(new News()
            {
                Id = Guid.NewGuid(),
                Title = "title 2",
                ShortDescription = "short description 2",
                Content = "content 2",
                IsPublish = true,
                NumberOfView = 0,
                PublishedPerson = "published person 2",
                PublishedDate = DateTime.Now,
                CreatedDate = DateTime.Now,
                CreatedUser = "created user 2",
                UpdatedDate = DateTime.Now,
                UpdatedUser = "updated user 2",
                Image = new byte[] { 123, 123, 123 }
            });
        }

        private void AddPoll()
        {
            PollList = new List<Poll>();

            PollList.Add(new Poll()
            {
                Id = Guid.NewGuid(),
                Name = "poll name 1",
                Value = 0,
                VoteDate = DateTime.Now,
                WhoVote = "who vote 1",
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "created user 1",
                UpdatedUser = "updated user 1"
            });

            PollList.Add(new Poll()
            {
                Id = Guid.NewGuid(),
                Name = "poll name 2",
                Value = 0,
                VoteDate = DateTime.Now,
                WhoVote = "who vote 2",
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "created user 2",
                UpdatedUser = "updated user 2"
            });
        }

        private void AddAudit()
        {
            AuditList = new List<Audit>();

            AuditList.Add(new Audit()
            {
                Id = Guid.NewGuid(),
                UserName = "user name 1",
                UserAction = "add category",
                DateEvent = DateTime.Now
            });

            AuditList.Add(new Audit()
            {
                Id = Guid.NewGuid(),
                UserName = "user name 2",
                UserAction = "update category",
                DateEvent = DateTime.Now
            });
        }

        private void AddUser()
        {
            UserList = new List<User>();

            UserList.Add(new User()
            {
                Id = Guid.NewGuid(),
                UserName = "username1",
                Password = "123456",
                IsAlive = true,
                Address = new Address("street 1", "ward 1", "district 1"),
                Description = "description 1",
                BelongToRole = 1,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now
            });

            UserList.Add(new User()
            {
                Id = Guid.NewGuid(),
                UserName = "username2",
                Password = "123456",
                IsAlive = true,
                Address = new Address("street 2", "ward 2", "district 2"),
                Description = "description 2",
                BelongToRole = 1,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now
            });
        }

        private void AddResources()
        {
            ResourceList = new List<Resources>();

            ResourceList.Add(new Resources()
            {
                Id = Guid.NewGuid(),
                ResourceType = "resource type 1",
                CultureCode = "en-GB",
                ResourceKey = "resource key 1",
                ResourceValue = "resource value 1",
                CreatedDate = DateTime.Now,
                CreatedUser = "test user",
                UpdatedDate = DateTime.Now,
                UpdatedUser = "test user"
            });

            ResourceList.Add(new Resources()
            {
                Id = Guid.NewGuid(),
                ResourceType = "resource type 2",
                CultureCode = "en-US",
                ResourceKey = "resource key 2",
                ResourceValue = "resource value 2",
                CreatedDate = DateTime.Now,
                CreatedUser = "test user",
                UpdatedDate = DateTime.Now,
                UpdatedUser = "test user"
            });
        }
    }
}
